home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Demos / Extend 3.0 Demo / Demo Libraries / Demo Custom Blocks Lib / Demo Custom Blocks Lib.rsrc / MODL_11407_Fish < prev    next >
Encoding:
Text File  |  1994-06-22  |  4.2 KB  |  193 lines

  1. **    Copyright ©1989, 1990 by Imagine That, Inc.
  2. **    All Rights Reserved.
  3. **    Extend EcoSphere Lib, Fauna universalis; Bob Diamond
  4. **        modified     7/21/90 BD
  5. ** creature[n][0] is the current starving age of the creature
  6. ** creature[n][1] is the current breeding age of the creature
  7. ** if these ages become greater or equal to zero, the creature
  8. ** starves or breeds as required
  9.  
  10. Integer    Creature[][2], initAlive, MaxCreatures, starveMn;
  11. integer    breedR, minSv, starveR, alive, breedMn, TwoMaxCreatures;
  12. integer    preyConnected;
  13.  
  14. Constant NONE is -1000000;
  15.  
  16. on simulate
  17. {
  18. integer    foods, new, i, j, starveAge, eaten, dummyInt;
  19. integer    diedNow, breedAge, foodEaten, feedAll, NewStarveAge;
  20.  
  21. ** how many food units came in the F connector
  22. foods = EatFoodOutIn/foodMult+0.5;
  23.  
  24. ** feed our creatures with food units
  25. feedAll = FALSE;
  26. foodEaten = 0;
  27. if (foods < TwoMaxCreatures)
  28.     {
  29.     for (i=0; i<foods; i++)
  30.         {
  31.         j = random(MaxCreatures);
  32.         starveAge = Creature[j][0];
  33.         if (starveAge != NONE)
  34.             {
  35.             Creature[j][0] -= (starveMn+random(starveR));    ** feed creature, age 0
  36.             foodEaten = foodEaten+1;
  37.             }
  38.         }
  39.     }
  40. else
  41.     feedAll = TRUE;    ** more food than 2 times the population
  42.  
  43. ** now, see if any eaten by a predator to the right
  44. if (currentStep == 0 OR NOT preyConnected)    ** none eaten initially
  45.     eaten = 0;
  46. else
  47.     eaten = alive-minSv-SupplyFoodOutIn; ** live ones eaten
  48.  
  49. i = 0;
  50. while (eaten > 0 AND i<MaxCreatures)
  51.     {
  52.     if (Creature[i][0] != NONE)
  53.         {
  54.         Creature[i][0] = NONE;    ** remove eaten creature
  55.         eaten--;    ** decrement eaten
  56.         alive--;    ** decrement alive
  57.         }
  58.     i++;            ** increment index
  59.     }
  60.     
  61. diedNow = 0;
  62. new = 0;
  63. NewStarveAge = 0;
  64.  
  65. ** count, test the population for breeding, starving age
  66. for (i=0; i<MaxCreatures; i++)
  67.     {
  68.     starveAge = Creature[i][0];    ** current age for this one
  69.     
  70.     if (starveAge != NONE)    ** a living one
  71.         {
  72.         if (feedAll)    ** more than enough food
  73.             {
  74.             starveAge = -(starveMn+random(starveR));
  75.             Creature[i][0] = starveAge;
  76.             foodEaten = foodEaten+1;
  77.             }
  78.         else
  79.             {
  80.             starveAge = starveAge+1;
  81.             Creature[i][0] = starveAge;    ** age Creature
  82.             }
  83.             
  84.         if (starveAge >= 0)    ** greater than starving age
  85.             {
  86.             Creature[i][0] = NONE;    ** creature is removed
  87.             diedNow++;                ** increment diedNow
  88.             alive--;
  89.             }
  90.         else
  91.             {
  92.             breedAge = Creature[i][1];
  93.             breedAge = breedAge+1;
  94.             Creature[i][1] = breedAge;    ** age Creature
  95.     
  96.             if (breedAge >= 0)
  97.                 {
  98.                 Creature[i][1] = -(breedMn+random(breedR));    ** age Creature
  99.                 new = new+MeanOffspring;
  100.                 }
  101.             }
  102.         }
  103.     }
  104.  
  105. if (alive < minSv)
  106.     new = new+minSv-alive;    ** make sure we have minimum
  107.  
  108. ** now, place new Creatures
  109. i = 0;
  110. while (new > 0  && i<MaxCreatures)
  111.     {
  112.     if (Creature[i][0] == NONE)
  113.         {
  114.         Creature[i][0] = -(starveMn+random(starveR));        ** age Creature
  115.         Creature[i][1] = -(breedMn+random(breedR));    ** age Creature
  116.         new--;
  117.         alive++;    ** increment alive
  118.         }
  119.     i++;            ** increment index
  120.     }
  121.  
  122. AliveOut = alive;    ** for plotting the population
  123.  
  124. SupplyFoodOutIn = alive-minSv;    ** don't use min survivors
  125.  
  126. deadOutIn += diedNow*CarrionEquiv;    ** add dead to connector
  127.  
  128. EatFoodOutIn -= int(foodEaten*foodMult+0.5);    ** round it
  129. }
  130.  
  131.  
  132. on initsim
  133. {
  134. integer    i;
  135.     
  136. initAlive = initCreatures;
  137. alive = initAlive;
  138. MaxCreatures = maximumCreatures;
  139. TwoMaxCreatures = MaxCreatures*2;
  140.  
  141. breedMn = BreedMean*0.5+1;
  142. breedR = BreedMean+1;
  143.  
  144. starveMn = StarveMean*0.5+1;    ** compensation
  145. starveR = StarveMean;
  146.  
  147. minSv = minSurvive;
  148. makeArray(Creature, MaxCreatures);
  149. for (i=0; i<initAlive; i++)
  150.     {
  151.     Creature[i][0] = -(starveMn+random(starveR));    ** new Creature, age 1
  152.     Creature[i][1] = -(breedMn+random(breedR));    ** new Creature
  153.     }
  154.     
  155. for (i=initAlive; i<MaxCreatures; i++)
  156.     Creature[i][0] = NONE;    ** no Creature
  157. }
  158.  
  159.  
  160. on checkData    ** is dialog data valid for simulation?
  161. {
  162. if (novalue(initCreatures+maximumCreatures+BreedMean+StarveMean
  163.             +MeanOffspring+CarrionEquiv+foodMult+minSurvive))
  164.     abort;
  165.  
  166. if (initCreatures > maximumCreatures)
  167.     {
  168.     userError("initCreatures="+initCreatures+" maxCreatures="+maximumCreatures);
  169.     userError("Initial number of creatures must be less than or equal to the maximum number.");
  170.     abort;
  171.     }
  172.     
  173. preyConnected = SupplyFoodOutIn;    ** TRUE if connected
  174. }
  175.  
  176. on CreateBlock
  177. {
  178.  
  179. ** Default values
  180.  
  181. initCreatures = 10;
  182. BreedMean = 10;
  183. MeanOffspring = 10;
  184.  
  185. maximumCreatures = 300;
  186. minSurvive = 2;
  187. StarveMean = 3;
  188.  
  189. FoodMult = 1.0;
  190. CarrionEquiv = 10;
  191. }
  192.  
  193.